from sqlalchemy import create_engine
from model import Symbol, DailyData
from sqlalchemy.orm import sessionmaker


import json
import urllib2
import datetime


class BolsarScrapper():

    def __init__(self):
        self._url = "http://www.bolsar.com/VistasDL/PaginaGeneral.aspx/GetDataPack"
        self._data = '{"aEstadoTabla":[{"TablaNombre":"tbPymes","FiltroVto":"","FiltroEspecies":"","PagActualNro":"1","FilasxPagina":100,"MensajeNro":100,"HashCode":-1469464395},{"TablaNombre":"tbGeneral","FiltroVto":"T","FiltroEspecies":"","PagActualNro":"1","FilasxPagina":1000,"MensajeNro":26625,"HashCode":-1761192635}]}'
  

        self._engine = create_engine("sqlite:///bolsar.db", echo=False)
        Session = sessionmaker(bind=self._engine)
        self._session = Session()
        self._processedSymbols = []


    def _getBolsarData(self):

        req = urllib2.Request(self._url, self._data, {'Content-Type': 'application/json'})
        f = urllib2.urlopen(req)
        response = f.read()
        f.close()

        return response


    def _processTableGeneral(self, table):
        values = table["aTabla"]
        for value in values:
            self._processSymbol(value)
        

    def _processSymbol(self, value):
           
        symbol_name = value["Simbolo"]

        print "Processing '%s'" % symbol_name

        if symbol_name in self._processedSymbols:
            print "\tSkipping, duplicate."
            return 

        self._processedSymbols.append(symbol_name)

        symbol = self._session.query(Symbol).filter_by(name=symbol_name).first()
        if symbol == None:
            symbol = Symbol(symbol_name, symbol_name)
            self._session.add(symbol)

 
        date = datetime.datetime.now().date()

        dd = DailyData(symbol, date)

        dd.nominalBuyAmmount = value["CantidadNominalCompra"]
        dd.nominalSellAmmount = value["CantidadNominalVenta"]
        dd.amountOfOperations = value["CantidadOperaciones"]
        dd.buyPrice = value["PrecioCompra"]
        dd.sellPrice = value["PrecioVenta"]

        self._session.add(dd)



    def run(self):
        data = json.loads(self._getBolsarData())
        tables = data.get("d")


        for i in range(len(tables)):
            table = tables.pop()
            if table["TablaNombre"] == "tbGeneral":
                self._processTableGeneral(table)
        self._session.commit()
            


if __name__ == "__main__":

    bs = BolsarScrapper()
    bs.run()

   #print json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
